package org.csu.mypetstore.service;

import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.persistence.*;
import org.csu.mypetstore.util.UniqId;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
//import org.csu.mypetstore.util.UniqId;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@MapperScan
@EnableAutoConfiguration
public class OrderService {

    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SequenceMapper sequenceMapper;
    @Autowired
    private LineItemMapper lineItemMapper;

    @Transactional
    public void insertOrder(Order order) {
        order.setOrderId(getNextId("ordernum"));
        for (int i = 0; i < order.getLineItems().size(); i++) {
            LineItem lineItem = (LineItem) order.getLineItems().get(i);
            String itemId = lineItem.getItemId();
            Integer increment = new Integer(lineItem.getQuantity());
            Map<String, Object> param = new HashMap<String, Object>(2);
            param.put("itemId", itemId);
            param.put("increment", increment);
            itemMapper.updateInventoryQuantity(param);
        }

        orderMapper.insertOrder(order);
        orderMapper.insertOrderStatus(order);
        for (int i = 0; i < order.getLineItems().size(); i++) {
            LineItem lineItem = (LineItem) order.getLineItems().get(i);
            lineItem.setOrderId(order.getOrderId());
            lineItemMapper.insertLineItem(lineItem);
        }
    }

    @Transactional
    public Order getOrder(int orderId) {
        Order order = orderMapper.getOrder(orderId);
        order.setLineItems(lineItemMapper.getLineItemsByOrderId(orderId));

        for (int i = 0; i < order.getLineItems().size(); i++) {
            LineItem lineItem = (LineItem) order.getLineItems().get(i);
            Item item = itemMapper.getItem(lineItem.getItemId());
            item.setQuantity(itemMapper.getInventoryQuantity(lineItem.getItemId()));
            lineItem.setItem(item);
        }

        return order;
    }

    public List<Order> getOrdersByUsername(String username) {
        return orderMapper.getOrdersByUsername(username);
    }

    public int getNextId(String name) {
        Sequence sequence = new Sequence(name, -1);
        sequence = (Sequence) sequenceMapper.getSequence(sequence);
        if (sequence == null) {
            throw new RuntimeException("Error: A null sequence was returned from the database (could not get next " + name
                    + " sequence).");
        }
        Sequence parameterObject = new Sequence(name, sequence.getNextId() + 1);
        sequenceMapper.updateSequence(parameterObject);
        return sequence.getNextId();
    }

    public List<Order> getOrder(){
        return orderMapper.getOrder();
    }

    public List<Order> searchOrderList(String keyword) {
        return orderMapper.searchOrderList("%" + keyword.toLowerCase() + "%");
    }
}


//public class OrderService {
//    @Autowired
//    private OrderMapper orderMapper;
//    @Autowired
//    private LineItemMapper lineItemMapper;
//    @Autowired
//    private CartService cartService;
//
//
//    public List<Order> getAllOrderByUserId(String userId){
//        return orderMapper.findAllOrderByUserId(userId);
//    }
//
//    public Order getOrderByOrderId(String orderId){
//        return orderMapper.findOrderByOrderId(orderId);
//    }
//
//    public int orderNumbers(){
//        return orderMapper.orderNumbers();
//    }
//    public void AddOrder(Order order){
//        orderMapper.AddOrder(order);
//    }
//
//    public void insertLineItem(List<LineItem> lineItemList){
//        lineItemMapper.insertLineItem(lineItemList);
//    }
//
//    public void insertOrderStatus(Order order){
//        orderMapper.insertOrderStatus(order);
//    }
//
//    public List<LineItem> getLineItemList(String orderId){
//        return lineItemMapper.getLineItemList(orderId);
//    }
//
//    public Order readyOrder(Order order,double total,Account account){
//        String orderId= UniqId.getOrderNum();
//        order.setOrderId(orderId);
//        order.setCourier("UPS");
//        order.setTotalPrice(total);
//        order.setLocale("CA");
//        order.setStatus("n");
//        List<Cart>carts=cartService.getCartItemsByUsername(account);
//        order.setLineNum(carts.size());
//        return order;
//    }
//
//    //把订单插入数据库中
//    public void insertIntoData(Order order,Account account){
//        List<LineItem>lineItems=getCartLineItem(account,order.getOrderId());
//        insertLineItem(lineItems);
//        AddOrder(order);
//        insertOrderStatus(order);
//    }
//
//    public List<LineItem> getCartLineItem(Account account,String orderId){
//        List<Cart> carts=cartService.getCartItemsByUsername(account);
//        List<LineItem> lineItems=new ArrayList<>();
//        for (Cart cart:carts){
//            LineItem lineItem=new LineItem();
//            lineItem.setItemId(cart.getItem().getItemId());
//            lineItem.setLineNum(carts.size());
//            lineItem.setOrderId(orderId);
//            lineItem.setQuantity(cart.getQuantity());
//            lineItem.setUnitPrice(cart.getItem().getListprice());
//            lineItem.setItem(cart.getItem());
//            lineItems.add(lineItem);
//        }
//        return lineItems;
//    }
//
//    //分页将获取订单数据
//    public List<Order> getOrders(int current,int size){
//        return orderMapper.getOrders(current, size);
//    }
//
//    //根据id获取订单
//    public List<Order> getOrderById(String orderId,int page, int limit){
//        Map<String,Object> map=new HashMap<>();
//        map.put("orderId",orderId);
//        map.put("current",page);
//        map.put("limit",limit);
//        return orderMapper.getOrderById(map);
//    }
//
//    //获取数量根据id
//    public int countById(String orderId){
//        return orderMapper.countById(orderId);
//    }
//
//    //按照status来获取订单
//    public List<Order> getOrderByStatus(String status,int page, int limit){
//        Map<String,Object> map=new HashMap<>();
//        map.put("status",status);
//        map.put("current",page);
//        map.put("limit",limit);
//        return orderMapper.getOrderByStatus(map);
//    }
//
//    //获取数量根据id
//    public int countByStatus(String status){
//        return orderMapper.countByStatus(status);
//    }
//
//    //按照status和id来获取订单
//    public List<Order> getOrderByIdAndStatus(String status,String orderId,int page, int limit){
//        Map<String,Object> map=new HashMap<>();
//        map.put("status",status);
//        map.put("orderId",orderId);
//        map.put("current",page);
//        map.put("limit",limit);
//        return orderMapper.getOrderByIdAndStatus(map);
//    }
//
//    public int countByStatusAndId(String orderId,String status){
//        return orderMapper.countByStatusAndId(orderId, status);
//    }
//
//    //删除订单
//    public void deleteOrder(String orderId){
//        orderMapper.deleteOrder(orderId);
//        orderMapper.deleteOrderStatus(orderId);
//        lineItemMapper.deleteLineItem(orderId);
//    }
//
//    //修改订单状态
//    public void changeOrderStatus(String orderId,String status){
//        orderMapper.changeOrderStatus(orderId, status);
//    }
//
//
//    //修改订单
//    public void updateOrder(Order order){
//        orderMapper.updateOrder(order);
//    }
//
//    public void updateOrderStatus(Order order){
//        orderMapper.updateOrderStatus(order);
//    }
//
//    //写入支付时间和支付宝交易号
////    public void updatePayTime(String time,String id,String orderId){
////        orderMapper.updatePayTime(time, id, orderId);
////    }
//
//
//}

//@Service
//@MapperScan
//@EnableAutoConfiguration
//public class OrderService {

//    @Autowired
//    private AccountMapper accountMapper;

//    @Autowired
//    private OrderMapper orderMapper;
//
//    @Autowired
//    private LineItemMapper lineItemMapper;
//
//    @Autowired
//    private ItemMapper itemMapper;
//
//    @Autowired
//    private SequenceMapper sequenceMapper;

//    public Account getAccount(String username) {
//        return accountMapper.getAccountByUsername(username);
//    }
//
//    public Account getAccount(String username, String password) {
//        Account account = new Account();
//        account.setUsername(username);
//        account.setPassword(password);
//        return accountMapper.getAccountByUsernameAndPassword(account);
//    }
//
//    @Transactional
//    public void insertAccount(Account account) {
//        accountMapper.insertAccount(account);
//        accountMapper.insertProfile(account);
//        accountMapper.insertSignon(account);
//    }
//
//    @Transactional
//    public void updateAccount(Account account) {
//        accountMapper.updateAccount(account);
//        accountMapper.updateProfile(account);
//
//        if (account.getPassword() != null && account.getPassword().length() > 0) {
//            accountMapper.updateSignon(account);
//        }
//    }

//    public List<Order> getOrdersByUsername(String username) {
//        return orderMapper.getOrdersByUsername(username);
//    }
//
//    public Order getOrder(int orderId) {
//        Order order = orderMapper.getOrder(orderId);
//        order.setLineItems(lineItemMapper.getLineItemsByOrderId(orderId));
//
//        for (int i = 0; i < order.getLineItems().size(); i++) {
//            LineItem lineItem = (LineItem) order.getLineItems().get(i);
//            Item item = itemMapper.getItem(lineItem.getItemId());
//            item.setQuantity(itemMapper.getInventoryQuantity(lineItem.getItemId()));
//            lineItem.setItem(item);
//        }
//
//        return order;
//    }
//
//    public void insertOrder(Order order) {
//        order.setOrderId(getNextId("ordernum"));
//        for (int i = 0; i < order.getLineItems().size(); i++) {
//            LineItem lineItem = (LineItem) order.getLineItems().get(i);
//            String itemId = lineItem.getItemId();
//            Integer increment = new Integer(lineItem.getQuantity());
//            Map<String, Object> param = new HashMap<String, Object>(2);
//            param.put("itemId", itemId);
//            param.put("increment", increment);
//            itemMapper.updateInventoryQuantity(param);
//        }
//
//        orderMapper.insertOrder(order);
//        orderMapper.insertOrderStatus(order);
//        for (int i = 0; i < order.getLineItems().size(); i++) {
//            LineItem lineItem = (LineItem) order.getLineItems().get(i);
//            lineItem.setOrderId(order.getOrderId());
//            lineItemMapper.insertLineItem(lineItem);
//        }
//    }
//
//    private int getNextId(String name) {
//        Sequence sequence = new Sequence(name, -1);
//        sequence = sequenceMapper.getSequence(sequence);
//        if (sequence == null) {
//            throw new RuntimeException("Error: A null sequence was returned from the database (could not get next " + name
//                    + " sequence).");
//        }
//        Sequence parameterObject = new Sequence(name, sequence.getNextId() + 1);
//        sequenceMapper.updateSequence(parameterObject);
//        return sequence.getNextId();
//    }
//
////    public void updateOrder(Order order){
////        orderMapper.updateOrder(order);
////        orderMapper.updateOrderStatus(order);
////        System.out.println(order.getLineItems().size());
//////        for (int i = 0; i < order.getLineItems().size(); i++) {
//////            lineItemMapper.updateLineItem(order.getLineItems().get(i));
//////        }
////    }
//
////    public void removeOrder(int orderId){
////        orderMapper.deleteOrderById(orderId);
////        lineItemMapper.deleteLineItemById(orderId);
////    }
//
////    public List<Order> searchOrderList(String keyword){
////        return orderMapper.searchOrderList("%" + keyword.toLowerCase() + "%");
////    }
//}
